package cn.zhl.leetcode.链表;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2020-04-07 16:43
 **/
public class 约瑟夫问题 {
	/**
	 * 数数，从1-8，逢3退出，直到剩余一个
	 */
	public static void main(String[] args) {
		ListNode first = new ListNode(1);
		ListNode node = first;
		for (int i = 2; i < 9; i++) {
			ListNode listNode = new ListNode(i);
			node.next = listNode;
			node = listNode;
		}
		node.next = first;
		node = first;
		while (node != node.next) {
			ListNode prev = node.next;
			System.out.println(node.next.next.val);
			ListNode newStart = node.next.next.next;
			prev.next = newStart;
			node = newStart;
		}
		System.out.println(node.val);
	}
}
